from DrissionPage import ChromiumPage, ChromiumOptions
import time  # 导入time模块
import logging
from pprint import pprint


data = {
    "name": "Alice",
    "age": 30,
    "pets": [{"type": "dog", "name": "Buddy"}, {"type": "cat", "name": "Whiskers"}],
    "address": {"city": "Wonderland", "zipcode": "12345"},
}

# 测试pprint打印字典等数据很好看
pprint(data)

logging.basicConfig(
    level=logging.DEBUG,  # 设置日志级别
    filename="app.log",  # 日志文件
    filemode="a",  # 追加模式
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)

# 信息会打印到app.log文件中去
logging.debug("这是一条调试信息")
# eles()查询一组元素

# python-dotenv  必须先下载这个包
from dotenv import load_dotenv
import os

load_dotenv()  # 加载.env文件到环境变量中去
chrome_path = os.getenv("CHROME_PATH")
options = ChromiumOptions()

# print("chrome_path:", chrome_path)
# 设置浏览器的路径并且保存设置
options.set_browser_path(chrome_path)
options.set_user_data_path("./user_data")  # 指定一个本地文件夹保存用户数据

page = ChromiumPage()
page.get("https://www.lagou.com/wn/")
time.sleep(5)  # 等待2秒
page.refresh()  # 刷新页面，强制同步登录态
print("页面刷新完成")
search_input_sel = "#search_input"
search_button_sel = "#search_button"

page.ele(search_input_sel, timeout=15).input(os.getenv("SEARCH_KEYWORD"))
print("搜索输入框输入完成")
time.sleep(4)  # 输入后等待1秒
# 使用属性选择器定位搜索按钮元素，并执行点击操作
page.ele(search_button_sel).click()
print("点击搜索完成")


# 用于判断元素未出现的情况
# if not ele:
#     print('元素未找到')
